package me.zhanshi123.jdbcconnectionbridge;


import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import me.zhanshi123.jdbcconnectionbridge.util.PooledConnection;
import org.bukkit.plugin.Plugin;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ConnectionPoolHandler {
    private Map<String, HikariDataSource> pools = new HashMap<>();

    public ConnectionPoolHandler() {
        Set<String> identities = JdbcConnectionBridge.getConfigManager().getIdentities();
        identities.forEach(identity -> {
            HikariConfig config = new HikariConfig();
            List<String> mysql = JdbcConnectionBridge.getConfigManager().getMySQL(identity);
            config.setJdbcUrl(mysql.get(0));
            config.setUsername(mysql.get(1));
            config.setPassword(mysql.get(2));
            config.addDataSourceProperty("cachePrepStmts", "true");
            config.addDataSourceProperty("prepStmtCacheSize", "250");
            config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
            try {
                HikariDataSource dataSource = new HikariDataSource(config);
                pools.put(identity, dataSource);
            } catch (Exception e) {
                JdbcConnectionBridge.getInstance().getLogger().warning(identity + " 无法连接 原因: " + e.getMessage());
            }
        });
    }


    void closeAll() {
        pools.entrySet().forEach(entry -> entry.getValue().close());
    }

    public Connection getConnection(String identity) {
        try {
            HikariDataSource dataSource = pools.get(identity);

            if (dataSource == null) {
                return null;
            }
            Connection connection = dataSource.getConnection();
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定识别名的连接
     *
     * @param identity 识别名
     * @param plugin   插件主类
     * @return 指定识别名的连接
     */
    @Deprecated
    public Connection getConnection(String identity, Plugin plugin) {
        JdbcConnectionBridge.getInstance().plugins.add(plugin.getName());
        return getConnection(identity);
    }


    /**
     * 获取指定识别名的连接池连接<br/>
     * @param identity 识别名
     * @param plugin 插件主类
     * @return 指定识别名的连接池连接
     */
    public PooledConnection getPooledConnection(String identity, Plugin plugin) {
        JdbcConnectionBridge.getInstance().plugins.add(plugin.getName());
        return new PooledConnection(identity, getConnection(identity));
    }

    /**
     * 连接池连接使用后的回收
     * @param pooledConnection 要被回收的连接池连接
     */
    public void evictConnection(PooledConnection pooledConnection) {
        evictConnection(pooledConnection.getIdentity(), pooledConnection.getConnection());
    }

    /**
     * 使用指定识别名回收指定连接
     * @param identity 连接所属的识别名
     * @param connection 连接
     */
    public void evictConnection(String identity, Connection connection) {
        HikariDataSource dataSource = pools.get(identity);
        if (dataSource == null) {
            return;
        }
        dataSource.evictConnection(connection);
    }
}
